// pages/supply-detail/supply-detail.js
import { Supply } from '../../models/supply'
import {check} from '../../util/identityCheck'
import {Location} from '../../util/location'
const location = new Location()

import Call from '../../util/call'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
import queryParams from '../../util/query'
import collectFun from '../../util/collect'
import {Payment} from "../../models/payment"

const paymentModel = new Payment()
const supplyModel = new Supply()

const app = getApp()
Page({
  data: {
    data: {},
    id: '',
    isIphoneX: app.globalData.isIPhoneX,
    canStick: false,
    showBackHome: false
  },

  onLoad (options) {
    if(!check())return
    wx.showLoading({
      title: '加载中'
    })
    this.setData({
      id: options.id || queryParams(decodeURIComponent(options.q), 'id'),
      showBackHome: !!options.ald_share_src
    })
    this.getDetail()
    this.checkStick()
  },

  getDetail () {
    supplyModel.detail({
      id: this.data.id
    }, res => {
      if (res.auditStatus == 0) {
        // setTimeout(() => {
        //   Notify({
        //     message: '正在审核中，请耐心等待回复！',
        //     duration: 30000,
        //     background: '#46E3BC'
        //   })
        // }, 1000)
        Dialog.alert({
          message: '正在审核中，请耐心等待回复！',
        }).then(() => {
          // on close
        })
      }
      wx.hideLoading()
      this.setData({
        data: res
      })
    })
  },

  onCollect() {
    collectFun('供应商', this.data.data.id, this.data.data.collectState, () => {
      this.setData({
        'data.collectState': !this.data.data.collectState
      })
    })
  },

  toMap(event){
    location.geocoder(event.target.dataset.address).then(res=>{
      if(!res.message){
        let latitude = res.lat
        let longitude = res.lng
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },

  call () {
    Call(this.data.id, '供应商')

    // wx.requestSubscribeMessage({
    //   tmplIds: ['6E4h4hY0mRpdOPOZLZj6EGV26CKx-3BoSJ3wwLcs3t4', 'gqiUsZicpSK12MajZO_xtLfDLXEjyvcTp51cs7GsqoY', '-DHKrW9DRvbg3sEoVRYtcV92AWETa-B9VW8Wst3WgMg'],
    // })
  },

  payTop () {
    // 付费置顶
    if(!wx.getStorageSync('userToken')){
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      wx.navigateTo({
        url: `/pages/login/login`
      })
      return
    }
    if (!this.data.canStick) {
      wx.showToast({
        title: '置顶名额已满，请待名额空余后再试！',
        icon: 'none'
      })
      return
    }
    let data = JSON.stringify({
      id: this.data.id
    })
    wx.navigateTo({
      url: `/pages/pay-join/pay-join?type=供应商&data=${data}&payType=付费置顶`
    })
  },

  checkStick () {
    // 检查是否可以置顶
    paymentModel.checkStick({
      type: '供应商'
    }, res => {
      this.setData({
        canStick: res
      })
    })
  },

  toNewPage (e) {
    let data = JSON.stringify(e.currentTarget.dataset.product)
    wx.navigateTo({
      url: `/packageA/pages/supply-goods/supply-goods?data=${data}`
    })
  },

  previewImg() {
    wx.previewImage({
      current: this.data.data.avatar,
      urls: [this.data.data.avatar]
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '供应商' + this.data.data.title,
      path: `/pages/supply-detail/supply-detail?id=${this.data.id}&otherId=${wx.getStorageSync('userInfo').user_id}&time=${+new Date()}`
    }
  }
})
